//
//  AreaControl.m
//  diaoyuren
//
//  Created by 钓鱼人 on 16/2/29.
//  Copyright © 2016年 game. All rights reserved.
//

#import "AreaControl.h"
#import "CGFdbmBase.h"
#import "CityModel.h"
#import "WeathCitysCtrl.h"
#import "SelectNavView.h"
#import "WetherModel.h"

@interface AreaControl ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *tableView;
@end

@implementation AreaControl

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBar.hidden = YES;
    [self NavView];
}

-(void)NavView{
    SelectNavView *nav = [[SelectNavView alloc]initWithFrame:CGRectMake(0, 0,SCREEN_WIDTH , 64)];
    
    nav.titile.alpha = 0;
    nav.share.alpha = 0;
    [nav.bacBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:nav];
}

-(void)back{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *selectString= [NSString stringWithFormat:@"SELECT * FROM region_area WHERE citycode = %@",self.code];
    
    [self.ares addObjectsFromArray:[CGFdbmBase SearchSelfPathSqlWhithPath:@"diaoyur_city" AndTabName:@"region_area" SelectSting:selectString]];
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 44, SCREEN_WIDTH, SCREEN_HEIGHT-44) style:UITableViewStylePlain];
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    self.tableView.dataSource = self;
    self.tableView.delegate  = self;
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    self.tableView.tableFooterView = [UIView new];
    [self.view addSubview:self.tableView];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.ares.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    CityModel *model = [[CityModel alloc]initWithDictionary:self.ares[indexPath.row] error:nil];
    cell.textLabel.text = model.name;
    cell.textLabel.font = [UIFont systemFontOfSize:SCREEN_WIDTH*0.04];
    cell.textLabel.alpha = 0.7;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    CityModel *model = [[CityModel alloc]initWithDictionary:self.ares[indexPath.row] error:nil];
  
    WeathCitysCtrl *cityCtl = [self.navigationController.viewControllers objectAtIndex:2];
    
    NSDictionary *areaDic = self.ares[indexPath.row];
    NSDictionary *cityDic = self.ares[0];
    [areaDic setValue:cityDic[@"weather_code"] forKey:@"weather_code"];
    [self.ares insertObject:areaDic atIndex:indexPath.row];
    
    NSDictionary *ddddDic = self.ares[indexPath.row];
    
    CityModel *mmmodel = [[CityModel alloc]initWithDictionary:ddddDic error:nil];
        if (cityCtl.cityArys.count==0) {
            
            [cityCtl.cityArys addObject:ddddDic];
            
        [self AchiveWeatherDataWhithCityName:mmmodel.name AndCode:mmmodel.code AndCtrl:cityCtl];
    }
    
    NSInteger count = 0;
    
    for (NSDictionary *dic in cityCtl.cityArys) {
        if ([dic[@"name"] isEqualToString:model.name]) {
            count = 2;
        }
    }
    if (count!=2) {
         [cityCtl.cityArys addObject:ddddDic];
        
        [self AchiveWeatherDataWhithCityName:mmmodel.name AndCode:mmmodel.code AndCtrl:cityCtl];
    }
    
    [cityCtl setCityArys:cityCtl.cityArys];
    
    [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:2] animated:YES];
}

//获取天气数据
-(void)AchiveWeatherDataWhithCityName:(NSString *)cityname AndCode:(NSString *)citycode AndCtrl:(WeathCitysCtrl *)cityCtl{
    [[Manager sharedManager]RequestWhithURL:[NSString stringWithFormat:weathUrl,citycode] AndParameter:nil AndCompletionType:^(id responseObject, DIEError *error) {
        
        WetherModel *model = [[WetherModel alloc]initWithDictionary:responseObject[@"data"] error:nil];
        
        if (model) {
             [cityCtl.cityDatadic addEntriesFromDictionary:@{cityname:model}];
        }else{
            
            
           __block NSString *dicKey;
            [cityCtl.cityDatadic enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
                dicKey = key;
                if (dicKey.length!=0) {
                    *stop = YES;
                }
            }];
            
            [cityCtl.cityDatadic addEntriesFromDictionary:@{cityname:cityCtl.cityDatadic[dicKey]}];
        }
    }];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return   SCREEN_HEIGHT*0.07;
}
@end
